/**
 * 初始化党风党纪管理详情对话框
 */
var PartDisciplineInfoDlg = {
    partDisciplineInfoData : {},
    editor: null,
    validateFields: {
        name: {
            validators: {
                notEmpty: {
                    message: '宪法名称不能为空'
                }
            }
        },
        content: {
        	vavalidators: {
        		notEmpty: {
        			message: '宪法内容不能为空'
        		}
        	}
        }
    }
};

/**
 * 清除数据
 */
PartDisciplineInfoDlg.clearData = function() {
    this.partDisciplineInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
PartDisciplineInfoDlg.set = function(key) {
	if(typeof value == "undefined"){
        if(typeof $("#" + key).val() =="undefined"){
            var str="";
            var ids="";
            $("input[name='"+key+"']:checkbox").each(function(){
                if(true == $(this).is(':checked')){
                    str+=$(this).val()+",";
                }
            });
            if(str){
                if(str.substr(str.length-1)== ','){
                    ids = str.substr(0,str.length-1);
                }
            }else{
                $("input[name='"+key+"']:radio").each(function(){
                    if(true == $(this).is(':checked')){
                        ids=$(this).val()
                    }
                });
            }
            this.partDisciplineInfoData[key] = ids;
        }else{
            this.partDisciplineInfoData[key]= $("#" + key).val();
        }
    }
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
PartDisciplineInfoDlg.get = function(key) {
    return $("#" + key).val();
}
/**
 * 关闭此对话框
 */
PartDisciplineInfoDlg.close = function() {
    parent.layer.close(window.parent.PartDiscipline.layerIndex);
}

/**
 * 收集数据
 */
PartDisciplineInfoDlg.collectData = function() {
	this.partDisciplineInfoData['content'] = PartDisciplineInfoDlg.editor.txt.html();
	this
    .set('id')
    .set('name')
    .set('category')
    .set('sort');
}
/**
 * 验证数据是否为空
 */
PartDisciplineInfoDlg.validate = function () {
    $('#partDisciplineInfoForm').data("bootstrapValidator").resetForm();
    $('#partDisciplineInfoForm').bootstrapValidator('validate');
    return $("#partDisciplineInfoForm").data('bootstrapValidator').isValid();
};
/**
 * 提交添加
 */
PartDisciplineInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/partDiscipline/add", function(data){
        Feng.success("添加成功!");
        window.parent.PartDiscipline.table.refresh();
        PartDisciplineInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.partDisciplineInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
PartDisciplineInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/partDiscipline/update", function(data){
        Feng.success("修改成功!");
        window.parent.PartDiscipline.table.refresh();
        PartDisciplineInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.partDisciplineInfoData);
    ajax.start();
}

$(function() {
	Feng.initValidator("partDisciplineInfoForm", PartDisciplineInfoDlg.validateFields);
	 //初始化编辑器
	    var E = window.wangEditor;
	    var editor = new E('#editor');
	    editor.create();
	    editor.txt.html($("#contentVal").val());
	    PartDisciplineInfoDlg.editor = editor;
});
